$(document).ready(function(){
var table;
var editfag = false;
var gtime = Date.now || function(){
    return +new Date;
};
$(function() {
   table =  $('#example').dataTable( {
    	//"sAjaxSource": "./admin!getAllUsers",
    	  ajax: {
                url: "admin!getAllUsers"
            },
         serverSide: true,
         columns: [
                    {"data": "uid"},
                    {"data": "uname"},
                    {"data": "uemail"},
                    { "data": "point" },
                    { "data": "class" },
                    { "data": "regdate" },
                    { "data": "status" },
                    { "data": "group" },
                    { "data": null }
           ],
         columnDefs: [
			{
				targets:[8],
				"bSortable": false,
				"data": null,
				"render": function (data, type, row) {
				return '<div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">'
			+'<a class="blue" href="#" id="view"  name="'+data['uid']+'" ><i class="icon-zoom-in bigger-130"></i></a>'+ 
				   '<a class="green" href="#" name="'+data['uid']+'" id="edit"><i class="icon-pencil bigger-130"></i></a>' +
				   '<a class="red" href="#" id="del" name="'+data['uid']+'"><i class="icon-trash bigger-130"></i></a></div>';
			}
			},{
				targets:[6],
				"bSortable": false,
				"data": null,
				"render": function (data, type, row) {
					return '<label><input  name="status" class="ace ace-switch ace-switch-2" type="checkbox" >' +
							'<span class="lbl"></span></label>';
				}
				
			}
                  ],
             
                  "dom": "<'row'<'col-xs-4'l><'#mytool.col-xs-2'><'col-xs-6'f>r>" +
                      "t" +
                      "<'row'<'col-xs-6'i><'col-xs-6'p>>",
              initComplete: function () {
                $("#mytool").append('<button type="button" class="btn btn-default btn-sm right" id="add" >添加</button>');
                $("#add").click(add);
              }
              
                     
    } );
  
  
    /**
     * 初始化基础数据
     */

    function ajax(id) {
    	//alert(editfag);
    	if(editfag){
    		//当当前状态可编辑 ，先查询给后传多个数据，保存
    		
    	}else{
    		//当当前状态不可编辑的时候，仅做查询
    		
    	}
    	
        $.ajax({
	       	 url:"admin!getAllUsers",
	       	 data:{
	       		 "uid":id
	       		 },
	       		 async: false,			//是否异步
	       		 cache: false,			//是否缓存
	       		 dataType:"json",
	       		 error:function(XMLResponse){alert(XMLResponse.responseText);}, 
	       		 success: function (data) {
	       			//var jsondata = JSON.parse(data);
	       			$("#myModalLabel").text("修改");
	       		    $("#name").val(data.uname).attr("disabled",true);
	       		    $("#position").val(data.regdate);
	       		    $("#salary").val(data.uemail);
	       		    $("#start_date").val(data.class);
	       		    $("#office").val(data.point);
	       		    $("#extn").val(data.uid);
	       		    $("#myModal").modal("show");
	       		 }
	         });
    }
    
    /**
     *编辑方法
     **/
    function add() {
        console.log(name);
        editFlag = true;
        $("#myModalLabel").text("添加");
        $("#name").val("").attr("disabled",false);
        $("#position").val("");
        $("#salary").val("");
        $("#start_date").val("");
        $("#office").val("");
        $("#extn").val("");
        $("#myModal").modal("show");
    }
   
 // 初始化刪除按钮
    $('#example tbody').on('click', '#del', function(e) {
        e.preventDefault();
        var id = $(this)[0].name; 
      //  alert("id 为："+name );
      $.ajax({
    	 url:"admin!getAllUsers",
    	 data:{
    		 "uid":id
    		 },
    		 async: false,			//是否异步
    		 cache: false,			//是否缓存
    		 dataType:"json",
    		 error:function(XMLResponse){alert(XMLResponse.responseText);}, 
    		 success: function (data) {
    			//var jsondata = JSON.parse(data);
    			alert(data.uid);
    		 }
    	  
      });
     
    })
    .on('click', '#edit', function(e){ 
    	  e.preventDefault();
    	  var id = $(this)[0].name; 
    	  editfag = true;
    	  ajax(id);
	  
    })
    .on('click', '#view', function(e) {
        e.preventDefault();
        var id = $(this)[0].name; 
       // var name = $('tr').siblings($).first();
        ajax(id);
        
    } );
} );

});